home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: const pointer confusion...
- Date: 26 Mar 1996 19:00:15 -0600
- Organization: Los Alamos National Laboratory
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4ja3uv$pf9@solutions.solon.com>
- References: <4j06gm$7oa@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4j06gm$7oa@solutions.solon.com>
- "Reed R. Mangino" <mangino@planet.net> writes:
-
- RM: Could someone please straighten me out on this:
- RM:
- RM: 1) const int *p = 10;
-
- If this compiles without warning, your compiler is not standard
- conforming. Let us forget the =0 for now.
-
- RM: p is a constant pointer to an int, right? While p can be made to
- RM: point to something else, *p can never be assigned to, right?
-
- Almost correct. First, what you describe is named pointer to const
- int: a const pointer to int is what you describe in (2) below.
-
- Second, *p cannot be assigned to, but it may be possible to assign to
- the same object otherwise. Thus,
-
- int x; /* x is modifiable */
- const int *p;
-
- p = &x; /* a pointer to const int _can_ point at non-const int, other
- way round is dangerous. */
- /* *p = 3 is an error */
- x = 3; /* this is not an error */
-
- RM:
- RM: 2) int *const p;
- RM: p is a pointer to an integer. *p can be assigned to, but p can
- RM: never be made to point to another address in memory, right?
-
- And as you don't initialize it either, it either is a null pointer (if
- that declaration appears outside any function), or has indeterminate
- value. So, do not try to use *p at all :-)
-
- RM:
- RM: 3) int const *p;
- RM: What the heck is this? I can't find anything like this in my
- RM: books, but my compiler thinks everything is hunky doory!???
-
- const qualifies the preceding thing, except when at the beginning. So,
- int const *p is the same as const int *p.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-